package cn.hh.ymcc.context;

import cn.hh.ymcc.dto.LoginInfoDto;
import com.alibaba.fastjson.JSON;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;

/**
 *  登录上下文
 */
public class LoginInfoContext {

    public static LoginInfoDto  loginInfo() {
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        String principal = (String) authentication.getPrincipal();
        if (StringUtils.isEmpty(principal)){ // 如果获取到的登录用户为空
            return null; // 直接return

        }
        LoginInfoDto loginInfoDto = JSON.parseObject(principal, LoginInfoDto.class);
        return loginInfoDto;

    }
}
